
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The form rendering API &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Widgets" href="widgets.html" />
    <link rel="prev" title="Formset Functions" href="formsets.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="formsets.html" title="Formset Functions">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="widgets.html" title="Widgets">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-forms-renderers">
            
  <div class="section" id="s-module-django.forms.renderers">
<span id="s-the-form-rendering-api"></span><span id="module-django.forms.renderers"></span><span id="the-form-rendering-api"></span><h1>The form rendering API<a class="headerlink" href="#module-django.forms.renderers" title="Permalink to this headline">¶</a></h1>
<p>Django’s form widgets are rendered using Django’s <a class="reference internal" href="../../topics/templates.html"><span class="doc">template engines
system</span></a>.</p>
<p>The form rendering process can be customized at several levels:</p>
<ul class="simple">
<li>Widgets can specify custom template names.</li>
<li>Forms and widgets can specify custom renderer classes.</li>
<li>A widget’s template can be overridden by a project. (Reusable applications
typically shouldn’t override built-in templates because they might conflict
with a project’s custom templates.)</li>
</ul>
<div class="section" id="s-the-low-level-render-api">
<span id="s-low-level-widget-render-api"></span><span id="the-low-level-render-api"></span><span id="low-level-widget-render-api"></span><h2>The low-level render API<a class="headerlink" href="#the-low-level-render-api" title="Permalink to this headline">¶</a></h2>
<p>The rendering of form templates is controlled by a customizable renderer class.
A custom renderer can be specified by updating the <a class="reference internal" href="../settings.html#std:setting-FORM_RENDERER"><code class="xref std std-setting docutils literal notranslate"><span class="pre">FORM_RENDERER</span></code></a>
setting. It defaults to
<code class="docutils literal notranslate"><span class="pre">'</span></code><a class="reference internal" href="#django.forms.renderers.DjangoTemplates" title="django.forms.renderers.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.forms.renderers.DjangoTemplates</span></code></a><code class="docutils literal notranslate"><span class="pre">'</span></code>.</p>
<p>You can also provide a custom renderer by setting the
<a class="reference internal" href="api.html#django.forms.Form.default_renderer" title="django.forms.Form.default_renderer"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Form.default_renderer</span></code></a> attribute or by using the <code class="docutils literal notranslate"><span class="pre">renderer</span></code> argument
of <a class="reference internal" href="widgets.html#django.forms.Widget.render" title="django.forms.Widget.render"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Widget.render()</span></code></a>.</p>
<p>Use one of the <a class="reference internal" href="#built-in-template-form-renderers"><span class="std std-ref">built-in template form renderers</span></a> or implement your own. Custom renderers
must implement a <code class="docutils literal notranslate"><span class="pre">render(template_name,</span> <span class="pre">context,</span> <span class="pre">request=None)</span></code> method. It
should return a rendered templates (as a string) or raise
<a class="reference internal" href="../../topics/templates.html#django.template.TemplateDoesNotExist" title="django.template.TemplateDoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code></a>.</p>
</div>
<div class="section" id="s-built-in-template-form-renderers">
<span id="s-id1"></span><span id="built-in-template-form-renderers"></span><span id="id1"></span><h2>Built-in-template form renderers<a class="headerlink" href="#built-in-template-form-renderers" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-djangotemplates">
<span id="djangotemplates"></span><h3><code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code><a class="headerlink" href="#djangotemplates" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.renderers.DjangoTemplates">
<em class="property">class </em><code class="descname">DjangoTemplates</code><a class="reference internal" href="../../_modules/django/forms/renderers.html#DjangoTemplates"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.forms.renderers.DjangoTemplates" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This renderer uses a standalone
<a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a>
engine (unconnected to what you might have configured in the
<a class="reference internal" href="../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting). It loads templates first from the built-in form
templates directory in <code class="docutils literal notranslate"><span class="pre">django/forms/templates</span></code> and then from the installed
apps’ templates directories using the <a class="reference internal" href="../templates/api.html#django.template.loaders.app_directories.Loader" title="django.template.loaders.app_directories.Loader"><code class="xref py py-class docutils literal notranslate"><span class="pre">app_directories</span></code></a> loader.</p>
<p>If you want to render templates with customizations from your
<a class="reference internal" href="../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting, such as context processors for example, use the
<a class="reference internal" href="#django.forms.renderers.TemplatesSetting" title="django.forms.renderers.TemplatesSetting"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplatesSetting</span></code></a> renderer.</p>
</div>
<div class="section" id="s-jinja2">
<span id="jinja2"></span><h3><code class="docutils literal notranslate"><span class="pre">Jinja2</span></code><a class="headerlink" href="#jinja2" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.renderers.Jinja2">
<em class="property">class </em><code class="descname">Jinja2</code><a class="reference internal" href="../../_modules/django/forms/renderers.html#Jinja2"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.forms.renderers.Jinja2" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This renderer is the same as the <a class="reference internal" href="#django.forms.renderers.DjangoTemplates" title="django.forms.renderers.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a> renderer except that
it uses a <a class="reference internal" href="../../topics/templates.html#django.template.backends.jinja2.Jinja2" title="django.template.backends.jinja2.Jinja2"><code class="xref py py-class docutils literal notranslate"><span class="pre">Jinja2</span></code></a> backend. Templates
for the built-in widgets are located in <code class="docutils literal notranslate"><span class="pre">django/forms/jinja2</span></code> and installed
apps can provide templates in a <code class="docutils literal notranslate"><span class="pre">jinja2</span></code> directory.</p>
<p>To use this backend, all the widgets in your project and its third-party apps
must have Jinja2 templates. Unless you provide your own Jinja2 templates for
widgets that don’t have any, you can’t use this renderer. For example,
<a class="reference internal" href="../contrib/admin/index.html#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.admin</span></code></a> doesn’t include Jinja2 templates for its widgets
due to their usage of Django template tags.</p>
</div>
<div class="section" id="s-templatessetting">
<span id="templatessetting"></span><h3><code class="docutils literal notranslate"><span class="pre">TemplatesSetting</span></code><a class="headerlink" href="#templatessetting" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.forms.renderers.TemplatesSetting">
<em class="property">class </em><code class="descname">TemplatesSetting</code><a class="reference internal" href="../../_modules/django/forms/renderers.html#TemplatesSetting"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.forms.renderers.TemplatesSetting" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This renderer gives you complete control of how widget templates are sourced.
It uses <a class="reference internal" href="../../topics/templates.html#django.template.loader.get_template" title="django.template.loader.get_template"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_template()</span></code></a> to find widget
templates based on what’s configured in the <a class="reference internal" href="../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting.</p>
<p>Using this renderer along with the built-in widget templates requires either:</p>
<ul>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">'django.forms'</span></code> in <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> and at least one engine
with <a class="reference internal" href="../settings.html#std:setting-TEMPLATES-APP_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">APP_DIRS=True</span></code></a>.</p>
</li>
<li><p class="first">Adding the built-in widgets templates directory in <a class="reference internal" href="../settings.html#std:setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> of one of your template engines. To generate that path:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">django</span>
<span class="n">django</span><span class="o">.</span><span class="n">__path__</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;/forms/templates&#39;</span>  <span class="c1"># or &#39;/forms/jinja2&#39;</span>
</pre></div>
</div>
</li>
</ul>
<p>Using this renderer requires you to make sure the form templates your project
needs can be located.</p>
</div>
</div>
<div class="section" id="s-context-available-in-widget-templates">
<span id="context-available-in-widget-templates"></span><h2>Context available in widget templates<a class="headerlink" href="#context-available-in-widget-templates" title="Permalink to this headline">¶</a></h2>
<p>Widget templates receive a context from <a class="reference internal" href="widgets.html#django.forms.Widget.get_context" title="django.forms.Widget.get_context"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Widget.get_context()</span></code></a>. By
default, widgets receive a single value in the context, <code class="docutils literal notranslate"><span class="pre">widget</span></code>. This is a
dictionary that contains values like:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">name</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">value</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">attrs</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">is_hidden</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">template_name</span></code></li>
</ul>
<p>Some widgets add further information to the context. For instance, all widgets
that subclass <code class="docutils literal notranslate"><span class="pre">Input</span></code> defines <code class="docutils literal notranslate"><span class="pre">widget['type']</span></code> and <a class="reference internal" href="widgets.html#django.forms.MultiWidget" title="django.forms.MultiWidget"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultiWidget</span></code></a>
defines <code class="docutils literal notranslate"><span class="pre">widget['subwidgets']</span></code> for looping purposes.</p>
</div>
<div class="section" id="s-overriding-built-in-widget-templates">
<span id="s-id2"></span><span id="overriding-built-in-widget-templates"></span><span id="id2"></span><h2>Overriding built-in widget templates<a class="headerlink" href="#overriding-built-in-widget-templates" title="Permalink to this headline">¶</a></h2>
<p>Each widget has a <code class="docutils literal notranslate"><span class="pre">template_name</span></code> attribute with a value such as
<code class="docutils literal notranslate"><span class="pre">input.html</span></code>. Built-in widget templates are stored in the
<code class="docutils literal notranslate"><span class="pre">django/forms/widgets</span></code> path. You can provide a custom template for
<code class="docutils literal notranslate"><span class="pre">input.html</span></code> by defining <code class="docutils literal notranslate"><span class="pre">django/forms/widgets/input.html</span></code>, for example.
See <a class="reference internal" href="widgets.html#built-in-widgets"><span class="std std-ref">Built-in widgets</span></a> for the name of each widget’s template.</p>
<p>To override widget templates, you must use the <a class="reference internal" href="#django.forms.renderers.TemplatesSetting" title="django.forms.renderers.TemplatesSetting"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplatesSetting</span></code></a>
renderer. Then overriding widget templates works <a class="reference internal" href="../../howto/overriding-templates.html"><span class="doc">the same as</span></a> overriding any other template in your project.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The form rendering API</a><ul>
<li><a class="reference internal" href="#the-low-level-render-api">The low-level render API</a></li>
<li><a class="reference internal" href="#built-in-template-form-renderers">Built-in-template form renderers</a><ul>
<li><a class="reference internal" href="#djangotemplates"><code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a></li>
<li><a class="reference internal" href="#jinja2"><code class="docutils literal notranslate"><span class="pre">Jinja2</span></code></a></li>
<li><a class="reference internal" href="#templatessetting"><code class="docutils literal notranslate"><span class="pre">TemplatesSetting</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#context-available-in-widget-templates">Context available in widget templates</a></li>
<li><a class="reference internal" href="#overriding-built-in-widget-templates">Overriding built-in widget templates</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="formsets.html"
                        title="previous chapter">Formset Functions</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="widgets.html"
                        title="next chapter">Widgets</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/forms/renderers.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="formsets.html" title="Formset Functions">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="widgets.html" title="Widgets">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>